package com.wlhse.controller;

import com.wlhse.dto.inDto.TaskManagementDto;
import com.wlhse.dto.getDto.TaskGet;

import com.wlhse.service.TaskManageService;
import com.wlhse.util.GetCurrentUserIdUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;


@RestController("TaskManageController")
@RequestMapping("/api/v1")
//任务管理
public class TaskManageController {

    @Resource
    private TaskManageService taskManageService;

    @Resource
    private GetCurrentUserIdUtil getCurrentUserIdUtil;

    @RequestMapping(value = "/tasks", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    public String newTask(@RequestBody(required = false) TaskManagementDto taskManagementDto, HttpServletRequest request) {
        taskManagementDto.setPublisherID(getCurrentUserIdUtil.getUserId(request));
        return taskManageService.addTask(taskManagementDto);
    }

    @RequestMapping(value = "/tasks/{id}", method = RequestMethod.DELETE, produces = "application/json; charset=utf-8")
    public String deleteTask(@PathVariable int id) {
        return taskManageService.deleteTask(id);

    }

    @RequestMapping(value = "/tasks/{id}", method = RequestMethod.PUT, produces = "application/json; charset=utf-8")
    public String modifyTask(@PathVariable int id, @RequestBody(required = false) TaskManagementDto taskManagementDto, HttpServletRequest request) {
        taskManagementDto.setTaskID(id);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
        String addTaskTime = df.format(new Date());
        taskManagementDto.setIssueDate(addTaskTime);
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        return taskManageService.editTask(taskManagementDto,userId);
    }

    @RequestMapping(value = "/tasks", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnTasks(@ModelAttribute TaskGet taskGet) {
        return taskManageService.listTaskByCondition(taskGet.getIssueDate1(), taskGet.getIssueDate2(), taskGet.getStatus(), taskGet.getPageIdx(), taskGet.getPageSize(), taskGet.getType(), taskGet.getIsOrderBy(), null, null, null, null);
    }

    @RequestMapping(value = "/tasks/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnTasksByID(@PathVariable int id, @RequestParam(value = "isOrderBy", required = false) String isOrderBy) {
        return taskManageService.listTaskByID(id, isOrderBy, null, null);
    }

    @RequestMapping(value = "/tasks/LeaderID", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnLeaderIDTasks(@ModelAttribute TaskGet taskGet, HttpServletRequest request) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = df.format(new Date());
        return taskManageService.listTaskByCondition(taskGet.getIssueDate1(), taskGet.getIssueDate2(), taskGet.getStatus(), taskGet.getPageIdx(), taskGet.getPageSize(), taskGet.getType(), taskGet.getIsOrderBy(), getCurrentUserIdUtil.getUserId(request), null, format, "未下达");
    }

    @RequestMapping(value = "/tasks/PublishID", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnPublishIDTasks(@ModelAttribute TaskGet taskGet, HttpServletRequest request) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = df.format(new Date());
        return taskManageService.listTaskByCondition(taskGet.getIssueDate1(), taskGet.getIssueDate2(), taskGet.getStatus(), taskGet.getPageIdx(), taskGet.getPageSize(), taskGet.getType(), taskGet.getIsOrderBy(), null, getCurrentUserIdUtil.getUserId(request), format, null);
    }

    @RequestMapping(value = "/receive_tasks", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String returnReceiveTasks(@ModelAttribute TaskGet taskGet) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = df.format(new Date());
        return taskManageService.listTaskByCondition(taskGet.getIssueDate1(), taskGet.getIssueDate2(), taskGet.getStatus(), taskGet.getPageIdx(), taskGet.getPageSize(), taskGet.getType(), taskGet.getIsOrderBy(), null, null, format, "未下达");
    }

    @RequestMapping(value = "/person_task", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryTasksByPublishPerson(@ModelAttribute TaskManagementDto taskManagementDto, HttpServletRequest request) {
        Integer userId = getCurrentUserIdUtil.getUserId(request);
        taskManagementDto.setCheckPersonsID(userId);
        return taskManageService.queryTasksByPublishPerson(taskManagementDto);
    }

    @RequestMapping(value = "/person_task/{id}", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    public String queryTasksByPublishPersonByID(@PathVariable("id") Integer id) {
        return taskManageService.queryTasksByPublishPersonByID(id);
    }

}
